/*
 * SisAP - Sistema de Alocacao de Professores
 *
 * Copyright (C) 2011  Macambira Team.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.persistence;

import com.googlecode.conection.ConectaDB;
import com.googlecode.entity.Turma;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * Classe responsavel por fazer a persistencia com o banco de dados do turma
 *
 * @version 0.1 03 dec 2011
 * @author Jafet
 */
public class TurmaDAO {
    private static TurmaDAO TurmaDAO;
        
    public static TurmaDAO getInstance() {
            synchronized (TurmaDAO.class) {
                if (TurmaDAO == null) {
                    TurmaDAO = new TurmaDAO();
                }
            }
            return TurmaDAO;
    }
    
    public void CadastrarTurma (Turma turma) throws ClassNotFoundException, SQLException {
        PreparedStatement ps;
        try {
            Connection con = (Connection) ConectaDB.getInstance().getCon();
            ps = (PreparedStatement) con.prepareStatement("INSERT INTO turmas"
                    + "(idTurma, nome)"
                    + "VALUES (?,?)");
            ps.setString(1, turma.getId());
            ps.setString(2, turma.getNome());
            ps.execute();
            con.close();
            JOptionPane.showMessageDialog(null, "Cadastro realizado com Sucesso");
        }catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Turma não pode ser cadastrada");
            throw new RuntimeException(e);
        }
    }
    
    public void RemoverTurmaID (String idTurma) throws ClassNotFoundException, SQLException {
        PreparedStatement ps;
        try {
        Connection con = (Connection) ConectaDB.getInstance().getCon();
        ps = (PreparedStatement) con.prepareStatement("DELETE FROM turmas WHERE ='" + idTurma + "'");
        ps.execute();
        con.close();
        JOptionPane.showMessageDialog(null, "Removido com sucesso");
      } catch(SQLException e) {
          JOptionPane.showMessageDialog(null, "Turma não pode ser removido");
          throw new RuntimeException(e);
      }
    }
    
    public void RemoverTurmaNome (String nomeTurma) throws ClassNotFoundException, SQLException {
        PreparedStatement ps;
        try {
        Connection con = (Connection) ConectaDB.getInstance().getCon();
        ps = (PreparedStatement) con.prepareStatement("DELETE FROM turmas WHERE ='" + nomeTurma + "'");
        ps.execute();
        con.close();
        JOptionPane.showMessageDialog(null, "Removido com sucesso");
      } catch(SQLException e) {
          JOptionPane.showMessageDialog(null, "Turma não pode ser removido");
          throw new RuntimeException(e);
      }
    }
    
    public void EditarTurma (Turma turma) throws ClassNotFoundException, SQLException {
        PreparedStatement ps;
        try {
            Connection con = (Connection) ConectaDB.getInstance().getCon();
            ps = (PreparedStatement) con.prepareStatement("UPDATE turmas nome = ? WHERE idTurma = ?");
            ps.setString(1, turma.getNome());
            ps.setString(2, turma.getId());
            ps.execute();
            con.close();
            JOptionPane.showMessageDialog(null, "Alteração Realizada");
        } catch(SQLException e) {
            JOptionPane.showMessageDialog(null, "Alteração não foi realizada");
            throw new RuntimeException(e);
        }
    }
    
    public Turma ConsultaturmaID (String idTurma) throws ClassNotFoundException, SQLException {
        ResultSet rs;
        PreparedStatement ps;
        Turma t = new Turma();
        try {
            Connection con = (Connection) ConectaDB.getInstance().getCon();
            ps = (PreparedStatement) con.prepareStatement("SELECT * FROM turmas WHERE idTurma = ?");
            ps.setString(1, idTurma);
            rs = ps.executeQuery();
            while(rs.next()){
                t.setId(rs.getString("idTurma"));
                t.setNome(rs.getString("nome"));
            }
            con.close();
        } catch(SQLException e) {
            JOptionPane.showMessageDialog(null, "Não pode ser encontrado");
            throw new RuntimeException(e);
        }
        return t;
    }
    
    public Turma ConsultaturmaNome (String nomeTurma) throws ClassNotFoundException, SQLException {
        ResultSet rs;
        PreparedStatement ps;
        Turma t = new Turma();
        try {
            Connection con = (Connection) ConectaDB.getInstance().getCon();
            ps = (PreparedStatement) con.prepareStatement("SELECT * FROM turmas WHERE nome = ?");
            ps.setString(1, nomeTurma);
            rs = ps.executeQuery();
            while(rs.next()){
                t.setId(rs.getString("idTurma"));
                t.setNome(rs.getString("nome"));
            }
            con.close();
        } catch(SQLException e) {
            JOptionPane.showMessageDialog(null, "Não pode ser encontrado");
            throw new RuntimeException(e);
        }
        return t;
    }
    
    public List<Turma> ConsultaTodasTurmas () {
        List<Turma> turmas = new LinkedList<Turma>();
        ResultSet rs;
        PreparedStatement ps;
        Turma t = new Turma();
        try {
            Connection con = (Connection) ConectaDB.getInstance().getCon();
            ps = (PreparedStatement) con.prepareStatement("SELECT * FROM turmas WHERE nome = ?");
            //ps.setString(1, nomeTurma);
            rs = ps.executeQuery();
            while(rs.next()){
                t.setId(rs.getString("idTurma"));
                t.setNome(rs.getString("nome"));
                turmas.add(t);
            }
            con.close();
            return turmas;
        } catch(Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Não pode ser encontrado");
        }
        return turmas;
    }
}
